﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DotnetWinFormApp
{
    public partial class FCaptureImage : Form
    {
        //Boolean m_IsFullScreen = false;//标记是否全屏


        public FCaptureImage()
        {
            InitializeComponent();
        }

        private FormBorderStyle _style;
        private void FCaptureImage_Load(object sender, EventArgs e)
        {
            this._style = this.FormBorderStyle;
        }



        private void FCaptureImage_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            //this.SetVisibleCore(false);
            //this.FormBorderStyle = FormBorderStyle.None;
            // this.WindowState = FormWindowState.Maximized;
            //this.SetVisibleCore(true);

            //this.Visible = false;
            //var bmp = this.GetScreen();
            //this.BackgroundImage = bmp;
            //this.Visible = true;



        }



        private void button1_Click(object sender, EventArgs e)
        {
            //this.SetVisibleCore(false);

            //this.Visible = false;
            //var bmp = this.GetScreen();
            ////this.BackgroundImage = bmp;
            //this.pictureBox1.Image = bmp;
            //this.Visible = true;
            //this.FormBorderStyle = FormBorderStyle.None;
            //this.WindowState = FormWindowState.Maximized;


            //m_IsFullScreen = !m_IsFullScreen;//点一次全屏，再点还原。  
            //this.SuspendLayout();
            //if (m_IsFullScreen)//全屏 ,按特定的顺序执行
            //{
            //    SetFormFullScreen(m_IsFullScreen);
            //    this.FormBorderStyle = FormBorderStyle.None;
            //    this.WindowState = FormWindowState.Maximized;
            //    this.Activate();//
            //}
            //else//还原，按特定的顺序执行——窗体状态，窗体边框，设置任务栏和工作区域
            //{
            //    this.WindowState = FormWindowState.Normal;
            //    this.FormBorderStyle = FormBorderStyle.Sizable;
            //    SetFormFullScreen(m_IsFullScreen);
            //    this.Activate();
            //}
            //this.ResumeLayout(false);

        }

        /// <summary>  
        /// 设置全屏或这取消全屏  
        /// </summary>  
        /// <param name="fullscreen">true:全屏 false:恢复</param>  
        /// <param name="rectOld">设置的时候，此参数返回原始尺寸，恢复时用此参数设置恢复</param>  
        /// <returns>设置结果</returns>  
        public Boolean SetFormFullScreen(Boolean fullscreen)//, ref Rectangle rectOld
        {
            Rectangle rectOld = Rectangle.Empty;
            Int32 hwnd = 0;
            hwnd = FindWindow("Shell_TrayWnd", null);//获取任务栏的句柄

            if (hwnd == 0) return false;

            if (fullscreen)//全屏
            {
                ShowWindow(hwnd, SW_HIDE);//隐藏任务栏

                SystemParametersInfo(SPI_GETWORKAREA, 0, ref rectOld, SPIF_UPDATEINIFILE);//get屏幕范围
                Rectangle rectFull = Screen.PrimaryScreen.Bounds;//全屏范围
                SystemParametersInfo(SPI_SETWORKAREA, 0, ref rectFull, SPIF_UPDATEINIFILE);//窗体全屏幕显示
            }
            else//还原 
            {
                ShowWindow(hwnd, SW_SHOW);//显示任务栏
                SystemParametersInfo(SPI_SETWORKAREA, 0, ref rectOld, SPIF_UPDATEINIFILE);//窗体还原
            }
            return true;
        }

        #region user32.dll

        public const Int32 SPIF_UPDATEINIFILE = 0x1;
        public const Int32 SPI_SETWORKAREA = 47;
        public const Int32 SPI_GETWORKAREA = 48;
        public const Int32 SW_SHOW = 5;
        public const Int32 SW_HIDE = 0;

        [DllImport("user32.dll", EntryPoint = "ShowWindow")]
        public static extern Int32 ShowWindow(Int32 hwnd, Int32 nCmdShow);

        [DllImport("user32.dll", EntryPoint = "FindWindow")]
        private static extern Int32 FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll", EntryPoint = "SystemParametersInfo")]
        private static extern Int32 SystemParametersInfo(Int32 uAction, Int32 uParam, ref Rectangle lpvParam, Int32 fuWinIni);

        #endregion





        private void button2_Click(object sender, EventArgs e)
        {
            //this.SetVisibleCore(true);
            //this.FormBorderStyle = this._style;


            //this.TopMost = true;
            //this.Location = new Point(0, 0);
            //this.Size = new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
            //this.Visible = false;
            //var bmp = this.GetScreen();
            //this.BackgroundImage = bmp;
            //this.Visible = true;


            this.Visible = false;
            this.Size = new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
            var bmp = this.GetScreen();
            //this.BackgroundImage = bmp;
            this.pictureBox1.Image = bmp;
            this.Visible = true;
            this.FormBorderStyle = FormBorderStyle.None;
            this.WindowState = FormWindowState.Maximized;

        }

        public Bitmap GetScreen()
        {
            //获取整个屏幕图像,不包括任务栏
            Rectangle screenArea = Screen.GetWorkingArea(this);
            Size taskbarSize = getCurTaskbarSize();
            Bitmap bmp = new Bitmap(screenArea.Width + taskbarSize.Width, screenArea.Height + taskbarSize.Height);
            using (Graphics g = Graphics.FromImage(bmp))
            {
                g.CopyFromScreen(0, 0, 0, 0, new Size(screenArea.Width, screenArea.Height));
            }

            return bmp;
        }




        // get current taskbar size(width, height), support 4 mode: taskbar bottom/right/up/left

        public Size getCurTaskbarSize()
        {
            int width = 0, height = 0;

            if ((Screen.PrimaryScreen.Bounds.Width == Screen.PrimaryScreen.WorkingArea.Width) &&
                (Screen.PrimaryScreen.WorkingArea.Y == 0))
            {
                //taskbar bottom
                width = Screen.PrimaryScreen.WorkingArea.Width;
                height = Screen.PrimaryScreen.Bounds.Height - Screen.PrimaryScreen.WorkingArea.Height;
            }
            else if ((Screen.PrimaryScreen.Bounds.Height == Screen.PrimaryScreen.WorkingArea.Height) &&
                    (Screen.PrimaryScreen.WorkingArea.X == 0))
            {
                //taskbar right
                width = Screen.PrimaryScreen.Bounds.Width - Screen.PrimaryScreen.WorkingArea.Width;
                height = Screen.PrimaryScreen.WorkingArea.Height;
            }
            else if ((Screen.PrimaryScreen.Bounds.Width == Screen.PrimaryScreen.WorkingArea.Width) &&
                    (Screen.PrimaryScreen.WorkingArea.Y > 0))
            {
                //taskbar up
                width = Screen.PrimaryScreen.WorkingArea.Width;
                //height = Screen.PrimaryScreen.WorkingArea.Y;
                height = Screen.PrimaryScreen.Bounds.Height - Screen.PrimaryScreen.WorkingArea.Height;
            }
            else if ((Screen.PrimaryScreen.Bounds.Height == Screen.PrimaryScreen.WorkingArea.Height) &&
                    (Screen.PrimaryScreen.WorkingArea.X > 0))
            {
                //taskbar left
                width = Screen.PrimaryScreen.Bounds.Width - Screen.PrimaryScreen.WorkingArea.Width;
                height = Screen.PrimaryScreen.WorkingArea.Height;
            }

            return new Size(width, height);
        }



        // get current taskbar position(X, Y), support 4 mode: taskbar bottom/right/up/left
        public Point getCurTaskbarLocation()
        {
            int xPos = 0, yPos = 0;

            if ((Screen.PrimaryScreen.Bounds.Width == Screen.PrimaryScreen.WorkingArea.Width) &&
                (Screen.PrimaryScreen.WorkingArea.Y == 0))
            {
                //taskbar bottom
                xPos = 0;
                yPos = Screen.PrimaryScreen.WorkingArea.Height;
            }
            else if ((Screen.PrimaryScreen.Bounds.Height == Screen.PrimaryScreen.WorkingArea.Height) &&
                    (Screen.PrimaryScreen.WorkingArea.X == 0))
            {
                //taskbar right
                xPos = Screen.PrimaryScreen.WorkingArea.Width;
                yPos = 0;
            }
            else if ((Screen.PrimaryScreen.Bounds.Width == Screen.PrimaryScreen.WorkingArea.Width) &&
                    (Screen.PrimaryScreen.WorkingArea.Y > 0))
            {
                //taskbar up
                xPos = 0;
                yPos = 0;
            }
            else if ((Screen.PrimaryScreen.Bounds.Height == Screen.PrimaryScreen.WorkingArea.Height) &&
                    (Screen.PrimaryScreen.WorkingArea.X > 0))
            {
                //taskbar left
                xPos = 0;
                yPos = 0;
            }

            return new Point(xPos, yPos);
        }



        // get current right bottom corner position(X, Y), support 4 mode: taskbar bottom/right/up/left
        public Point getCornerLocation(Size windowSize)
        {
            int xPos = 0, yPos = 0;
            if ((Screen.PrimaryScreen.Bounds.Width == Screen.PrimaryScreen.WorkingArea.Width) &&
                (Screen.PrimaryScreen.WorkingArea.Y == 0))
            {
                //taskbar bottom
                xPos = Screen.PrimaryScreen.WorkingArea.Width - windowSize.Width;
                yPos = Screen.PrimaryScreen.WorkingArea.Height - windowSize.Height;
            }
            else if ((Screen.PrimaryScreen.Bounds.Height == Screen.PrimaryScreen.WorkingArea.Height) &&
                    (Screen.PrimaryScreen.WorkingArea.X == 0))
            {
                //taskbar right
                xPos = Screen.PrimaryScreen.WorkingArea.Width - windowSize.Width;
                yPos = Screen.PrimaryScreen.WorkingArea.Height - windowSize.Height;
            }
            else if ((Screen.PrimaryScreen.Bounds.Width == Screen.PrimaryScreen.WorkingArea.Width) &&
                    (Screen.PrimaryScreen.WorkingArea.Y > 0))
            {
                //taskbar up
                xPos = Screen.PrimaryScreen.WorkingArea.Width - windowSize.Width;
                yPos = Screen.PrimaryScreen.WorkingArea.Y;
            }

            else if ((Screen.PrimaryScreen.Bounds.Height == Screen.PrimaryScreen.WorkingArea.Height) &&
                    (Screen.PrimaryScreen.WorkingArea.X > 0))
            {

                //taskbar left
                xPos = Screen.PrimaryScreen.WorkingArea.X;
                yPos = Screen.PrimaryScreen.WorkingArea.Height - windowSize.Height;
            }

            return new Point(xPos, yPos);
        }


    }
}
